<template>
  <div class="page-header-warp">
    <div class="page-header-nav">
      <a href="index.html">首页</a>
      <a href="music.html">音乐</a>
      <a href="chat.html">聊天</a>
      <a href="demo.html">案例</a>
      <a href="webRtc.html">WebRtc</a>
    </div>
    <div class="weather-tool">
      <span>{{ city }}</span>
      <span class="weather-icon" :title="weather">
        <svg v-if="getWeatherIcon(weather)" class="icon" aria-hidden="true" :title="weather">
          <use xlink :href="'#'+getWeatherIcon(weather)" />
        </svg>
        <template v-else>
          {{ weather }}
        </template>
      </span>
      <span>{{ temp }}</span>
    </div>
  </div>
</template>

<script>
export default {
  name: 'PageHeader',
  data() {
    return {
      weatherMap: {
        '晴': 'icon-qingtian',
        '多云': 'icon-duoyun',
        '扬沙': 'icon-yangsha',
        '霾': 'icon-mai',
        '大雨': 'icon-dayu',
        '中雨': 'icon-zhongyu',
        '小雨': 'icon-xiaoyu',
        '雷阵雨': 'icon-leizhenyu',
        '浮尘': 'icon-fuchen',
        '雾': 'icon-wu',
        '大雪': 'icon-daxue',
        '中雪': 'icon-zhongxue',
        '小雪': 'icon-xiaoxue',
        '雨夹雪': 'icon-yujiaxue',
        '阴': 'icon-yintian'
      },
      weather: '',
      city: '',
      temp: ''
    }
  },
  created() {
    this.getWeather()
  },
  methods: {
    getWeather() {
      this.$http.jsonp('https://api.asilu.com/weather/')
        .then(({ body }) => {
          this.city = body.city
          this.temp = body.weather[0].temp
          this.weather = body.weather[0].weather
        })
    },
    getWeatherIcon(weather) {
      const weathers = weather.split('转')
      if (weathers.length > 0) {
        return this.weatherMap[weathers[0]]
      }
      return this.weatherMap[weather]
    }
  }
}
</script>

<style lang="less" scoped>
  .page-header-warp{
    height: 30px;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    background-color: rgba(0, 0, 0, 0.3);
    z-index: 999;
  }
  .weather-tool{
    line-height: 30px;
    position: absolute;
    height: 30px;
    font-size: 14px;
    color: #d9dce0;
    right: 15px;
  }
  .weather-icon{
    font-size: 14px;
    margin: 0 5px;
  }
  .page-header-nav{
    height: 30px;
    display: inline-block;
    padding: 0 15px;
    line-height: 30px;
    vertical-align: middle;
    float: left;
  }
  .page-header-nav a{
    text-decoration: none;
    margin-left: 10px;
    font-size: 14px;
    color: #f7f7f7;
  }
  .page-header-nav a:hover{
    color: #079fe6;
  }
  .icon {
    display: inline-block;
    width: 1em;
    height: 1em;
    vertical-align: -0.15em;
    fill: currentColor;
    overflow: hidden;
  }
</style>
